package afcp.alumni.action.page;

import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.security.Secure;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.ValidationErrors;
import net.sourceforge.stripes.validation.ValidationMethod;
import afcp.alumni.action.base.PageAction;
import afcp.alumni.model.db.Member;
import afcp.alumni.security.PasswordEncoder;
import afcp.alumni.service.MemberService;
import afcp.alumni.util.ReflectUtil;

@UrlBinding(value = MyspaceAction.URL)
@Secure(roles = PageAction.VISIBLE)
public class MyspaceAction extends PageAction {

	@Override
	public String getTitle() {
		return msg("myspace.title");
	}

	@Override
	protected String defaultJsp() {
		return JSP;
	}

	@Override
	public String getUrl() {
		return URL;
	}

	@Override
	public Resolution view() {
		return toJsp();
	}

	public static final String URL = "/myspace";
	private static final String JSP = "/WEB-INF/jsp/myspace.jsp";

	@SpringBean
	private MemberService memberService;
//	@SpringBean
//	private RoleDao roleDao;
	
	@Before(stages = LifecycleStage.EventHandling)
	private void initMe() {
		if (me == null) {
			me = context.getUser();
		}
	}
	
	@Validate(on="changePassword", required=true)
	private String oldPassword;
	@Validate(on="changePassword", required=true, minlength=6)
	private String newPassword;
	@Validate(on="changePassword", required=true)
	private String newPassword2;
	
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public void setNewPassword2(String newPassword2) {
		this.newPassword2 = newPassword2;
	}

	@ValidationMethod(on = "changePassword")
	public void validateNewPassword(ValidationErrors errors) {
		if (!context.getUser().getPassword()
				.equals(PasswordEncoder.hash(oldPassword))) {
			errors.add("oldPassword", new LocalizableError(
					"myspace.wrongoldpassword"));
		} else if (!newPassword.equals(newPassword2)) {
			errors.add("newPassword2",
					new LocalizableError("myspace.mustbesame"));
		}
	}
	
	public Resolution changePassword() {
		Member user = context.getUser();
		user.setPassword(newPassword);
		memberService.addOrUpdateMember(user);
		addFlashScope("myspace.changepassword.ok");
		return redirect();
	}

	@ValidateNestedProperties({
//			@Validate(field = "login.id", required = true, on = "updateMe"),
//			@Validate(field = "firstName", required = true, on = "updateMe"),
//			@Validate(field = "lastName", required = true, on = "updateMe"),
//			@Validate(field = "roles.id", required = true, on = "updateMe"),
//			@Validate(field = "birthDate", required = true, on = "updateMe"),
		@Validate(field = "email", required = true, on = "updateMe"),
	})
	private Member me = null;

	public Member getMe() {
		return me;
	}

	public void setMe(Member me) {
		this.me = me;
	}
	
	public Resolution updateMe() {
//		me.getLogin().setAncestorKey(roleDao.getEntityAncestor().getKey());
		Member user = context.getUser();
		ReflectUtil.copyMemberEditableFields(me, user);
		memberService.addOrUpdateMember(user);
		addFlashScope("myspace.updateMe.ok");
		return redirect();
	}

}
